﻿Imports System.Data.OleDb

Public Class MainForm
    Private DB As New clsDatabase
    Private Lingo As New clsLingo
    Private objXML As New clsXMLCore
    Private objXSQL As New clsXML2SQL
    Private XMLString As String = ""
    Private Is_Debug As Boolean = False   'Change to false in normal mode.

    Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Lingo.SetModelPath = My.Application.Info.DirectoryPath & "\models\"
        If Is_Debug = False Then
            Me.Width = 717
        Else
            Me.Width = 1117
            Me.Text = Me.Text & " - Debugging Mode"
        End If
    End Sub

    Private Sub btnParse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnParse.Click
        DataGridView.Rows.Clear()
        DataGridView.Refresh()
        Cursor.Current = Cursors.WaitCursor
        TSSLabel.Text = "Status: Generating Parsing Tree..."
        SS.Refresh()
        Dim S As New clsSentence
        Dim POSTxt As String = Lingo.POSTag(txtInput.Text)
        Dim ArrPOS As Object = POSTxt.Split(vbNewLine)
        Dim Line As String = ""
        TSSLabel.Text = "Status: Generating XML from Parsing Tree..."
        SS.Refresh()
        XMLString = Lingo.ShowParseXML(txtInput.Text)
        TSSLabel.Text = "Status: Generating SQL command from XML..."
        SS.Refresh()
        Dim txtSQL As String = objXSQL.GetSQLfromXML(XMLString)
        TSSLabel.Text = "Status: Executing SQL command..."
        SS.Refresh()

        'SQL Execution code gose here...
        Try
            Dim DBConnect As Object = DB.Connect ' Connection
            Dim DBCommand As Object = DB.ExecuteSQLCommand(txtSQL, DBConnect) 'OleDbDataAdapter

            Dim LoadDataSet As New DataSet

            DBCommand.Fill(LoadDataSet, "products")

            Dim RowCount As Long = LoadDataSet.Tables("products").Rows.Count
            Dim ColCount As Integer = LoadDataSet.Tables("products").Columns.Count

            If RowCount < 1 Then
                MsgBox("What do you mean?", MsgBoxStyle.Question, Me.Text)
                DB.DisConnect(DBConnect)
                Cursor.Current = Cursors.Default
                TSSLabel.Text = "Status: Ready."
                SS.Refresh()
            Else

                DataGridView.ColumnCount = ColCount

                For C As Integer = 0 To ColCount - 1
                    Dim ColName As String = LoadDataSet.Tables("products").Columns(C).Caption.ToString
                    DataGridView.Columns(C).HeaderText = ColName
                    DataGridView.Columns(C).Width = LoadDataSet.Tables("products").Columns(C).Caption.Length * 10
                Next

                'DataGridView.Rows.Add(RowCount)
                For I As Integer = 0 To RowCount - 1
                    With DataGridView
                        .Rows.Add()
                        For J As Integer = 0 To ColCount - 1
                            .Rows(I).Cells(J).Value = LoadDataSet.Tables("products").Rows(I).Item(J)
                        Next
                    End With
                Next
                LoadDataSet.Reset()
                DB.DisConnect(DBConnect)
            End If
        Catch ex As Exception
            If txtSQL = "" Then
                MsgBox("What do you mean?", MsgBoxStyle.Question, Me.Text)
            Else
                MsgBox("The application is not connected with the main database." & vbNewLine & "Please consult the Administrator", MsgBoxStyle.Exclamation, Me.Text)
            End If
            'MsgBox("The application is not connected with the main database." & vbNewLine & "Please consult the Administrator", MsgBoxStyle.Exclamation, Me.Text)
        Finally
            Cursor.Current = Cursors.Default
            TSSLabel.Text = "Status: Ready."
            SS.Refresh()
        End Try
    End Sub

    Private Sub btnFndSubj_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFndSubj.Click
        Cursor.Current = Cursors.WaitCursor
        TSSLabel.Text = "Status: Generating SQL..."
        SS.Refresh()
        txtSQL.Clear()
        Dim XMLTree As String = XMLString
        If XMLTree = "" Then
            MsgBox("Please generate the parse tree first.", MsgBoxStyle.Exclamation, Me.Text)
        Else
            txtSQL.Text = objXSQL.GetSQLfromXML(XMLTree)
        End If
        TSSLabel.Text = "Status: Ready."
        SS.Refresh()
        Cursor.Current = Cursors.Default
    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        End
    End Sub

    Private Sub btnGenParse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGenParse.Click
        Cursor.Current = Cursors.WaitCursor
        TSSLabel.Text = "Status: Generating the Parse Tree and XML..."
        SS.Refresh()
        Dim S As New clsSentence
        Dim POSTxt As String = Lingo.POSTag(txtInput.Text)
        Dim ArrPOS As Object = POSTxt.Split(vbNewLine)
        Dim Line As String = ""

        txtOut.Text = "Parse Tree: " & vbNewLine & Lingo.ShowParseTree(txtInput.Text)
        txtOut.Text = txtOut.Text & vbNewLine & "XML:" & vbNewLine & Lingo.ShowParseXML(txtInput.Text)
        XMLString = Lingo.ShowParseXML(txtInput.Text)
        Cursor.Current = Cursors.Default
        TSSLabel.Text = "Status: Ready."
        SS.Refresh()
    End Sub

    Private Sub btnDBConChk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDBConChk.Click
        Cursor.Current = Cursors.WaitCursor
        TSSLabel.Text = "Status: Testing Database Server Connection..."
        SS.Refresh()
        Dim TestDB As Boolean = DB.TestConnection
        If TestDB = True Then
            MsgBox("Database Server Connection OK.", MsgBoxStyle.Information, Me.Text)
        Else
            MsgBox("Database Server Connection Fail.", MsgBoxStyle.Exclamation, Me.Text)
        End If
        TSSLabel.Text = "Status: Ready."
        SS.Refresh()
        Cursor.Current = Cursors.Default
    End Sub

    Private Sub btnExecSQL_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExecSQL.Click
        TSSLabel.Text = "Status: Executing SQL Command..."
        SS.Refresh()
        Cursor.Current = Cursors.WaitCursor
        Try
            DataGridView.Rows.Clear()
            DataGridView.Refresh()
            'SQL Execution code gose here...

            Dim DBConnect As Object = DB.Connect ' Connection
            Dim DBCommand As Object = DB.ExecuteSQLCommand(txtSQL.Text, DBConnect) 'OleDbDataAdapter

            Dim LoadDataSet As New DataSet

            DBCommand.Fill(LoadDataSet, "products")

            Dim RowCount As Long = LoadDataSet.Tables("products").Rows.Count
            Dim ColCount As Integer = LoadDataSet.Tables("products").Columns.Count

            If RowCount < 1 Then
                MsgBox("What do you mean?", MsgBoxStyle.Question, Me.Text)
                DB.DisConnect(DBConnect)
                Cursor.Current = Cursors.Default
                TSSLabel.Text = "Status: Ready."
                SS.Refresh()
            Else

                DataGridView.ColumnCount = ColCount

                For C As Integer = 0 To ColCount - 1
                    Dim ColName As String = LoadDataSet.Tables("products").Columns(C).Caption.ToString
                    DataGridView.Columns(C).HeaderText = ColName
                    DataGridView.Columns(C).Width = LoadDataSet.Tables("products").Columns(C).Caption.Length * 10
                Next

                'DataGridView.Rows.Add(RowCount)
                For I As Integer = 0 To RowCount - 1
                    With DataGridView
                        .Rows.Add()
                        For J As Integer = 0 To ColCount - 1
                            .Rows(I).Cells(J).Value = LoadDataSet.Tables("products").Rows(I).Item(J)
                        Next
                    End With
                Next
                LoadDataSet.Reset()
                DB.DisConnect(DBConnect)
            End If
        Catch ex As Exception
            If txtSQL.Text = "" Then
                MsgBox("What do you mean?", MsgBoxStyle.Question, Me.Text)
            Else
                MsgBox("The application is not connected with the main database." & vbNewLine & "Please consult the Administrator", MsgBoxStyle.Exclamation, Me.Text)
            End If
        Finally
            Cursor.Current = Cursors.Default
            TSSLabel.Text = "Status: Ready."
            SS.Refresh()
        End Try
    End Sub

    Private Sub chkDebug_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkDebug.CheckedChanged
        If chkDebug.Checked = False Then
            Me.Width = 717
            Me.Text = "NLP: English2SQL v.1.0"
        Else
            Me.Width = 1117
            Me.Text = "NLP: English2SQL v.1.0 - Debugging Mode"
        End If
    End Sub
End Class
